function toggleEmailCodeBlock() {
    var emailCodeBlock = document.getElementById('emailCodeBlock');
    if (emailCodeBlock.style.display === 'none') {
        emailCodeBlock.style.display = 'block';
    } else {
        emailCodeBlock.style.display = 'none';
    }
}
toggleEmailCodeBlock();
$(function(){
    $("#sendcode").click(function(){
    	sendcode();
    	return false;
    })
    $("#reg").click(function(){
    	reg();
    	return false;
    })
})
function sendcode(){
	var email= $("#email").val(); 
    var username= $("#username").val();
    var qq= $("#qq").val();
    var phone= $("#phone").val();
    var code_input = $("#code_input").val();
    var agree = document.getElementById('agree').checked;
    if(agree === false){
         falsemsg(layer,"请先勾选服务条款",function(){
               refreshCaptcha();//刷新验证码
          }); 
          return false;
    }
	var ii = layer.load(5, {shade:[0.1,'#fff']});
	$.ajax({
	    type : "POST",
	    url : "Ajax.php",
	    data : {
	        act:'Sendcode',
	        email:email,
	        user:username,
	        qq:qq,
	        phone:phone,
	        code_input:code_input
	    },
	    dataType : 'json',
	    success : function(data){
	        layer.close(ii);
	        if(data.code == 200){
	            truemsg(layer,data.msg,function(){
	                toggleEmailCodeBlock();	
	                refreshCaptcha();//刷新验证码
	            });
	        }else{
	           falsemsg(layer,data.msg,function(){
	               refreshCaptcha();//刷新验证码
	           }); 
	        }
	    },
		error:function(data){
    		layer.close(ii);
    		falsemsg(layer,"服务器错误",function(){
	               refreshCaptcha();//刷新验证码
	       }); 
		}
	});
}

function reg(){
    var user=$("#username").val();
	var pass=$("#password").val();
	var qq=$("#qq").val();
	var email=$("#email").val();
	var code=$("#code").val();
	var phone= $("#phone").val();
	var ii = layer.load(5, {shade:[0.1,'#fff']});
	$.ajax({
	    type : "POST",
	    url : "./Ajax.php",
	    data : {
	        act:"Reg",
	        user,
	        pass,
	        qq,
	        email,
	        code,
	        phone
	    },
	    dataType : 'json',
		timeout: 15000, //ajax请求超时时间15s
		success : function(res){
		    layer.close(ii);
		    if(res.code == 200){
		        truemsg(layer,res.msg,function(){
	                 window.location.href="./login";
	            });
		        layer.msg(res.msg, {
						icon: 1,
						time: 1000
				},function(){
				    window.location.href="./login";
				});
		    }else if(res.code == 1){
		        layer.msg(res.msg,{
		            time: 1000
		        },function(){
		            var paymsg = '';
                    $.each(res.data.paytype, function(key, value) {
                        paymsg += '<button class="btn btn-default btn-block" onclick="payNow(\'' + res.data.trade_no + '\', \'' + value.name + '\')" style="margin-top:10px;"><img width="20" src="/Core/assets/icon/' + value.name + '.ico" class="logo">' + value.showname + '</button>';
                    });
                    
                    layer.alert('<center><h2>￥ ' + res.data.need + '</h2><hr>' + paymsg + '<hr>提示：支付完成后即可直接登录</center>', {
                        btn:[],
                        title:'支付确认页面',
                        closeBtn: false
                    });
		        });
		    }else{
		        falsemsg(layer,res.msg); 
		    }
		},
		error:function(res){
    		layer.close(ii);
    	    falsemsg(layer,"服务器错误"); 
		}
	});
}
 // 支付函数
function payNow(trade_no,type) {
    var url = './api.php?s=submit_pay&type=' + type + '&trade_no=' + trade_no;
    window.location.href = url;
}